using System.Globalization;
using System.Windows.Data;
using System.Windows.Markup;

namespace LocalizationWpfDemo.Localization;

public class LangExtension(string key) : MarkupExtension
{
    private string Key { get; } = key;

    public override object? ProvideValue(IServiceProvider serviceProvider)
    {
        // return GetLocalizedValue(Key);

        var binding = new Binding(nameof(LocalizationProvider.CurrentLanguage))
        {
            Mode = BindingMode.OneWay,
            Source = AppContainer.LocalizationProvider,
            UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
            Converter = new LangConverter(Key)
        };
        return binding.ProvideValue(serviceProvider);
    }

    // private string GetLocalizedValue(string key)
    // {
    //     return AppContainer.LocalizationProvider.GetLang(key);
    // }
}

public class LangConverter(string key) : IValueConverter
{
    private string Key { get; } = key;

    public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        return AppContainer.LocalizationProvider.GetLang(Key);
    }

    public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}
